package com.henghe.frame.mq.config.mqtt;

import com.henghe.frame.mq.util.ClassScaner;
import com.henghe.frame.mq.annotation.OrderHandlerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/31 15:12
 */
@Component
public class HandlerProcess implements BeanFactoryPostProcessor {

    @Value("${mqtt.base-package}")
    private String basePackage;
    public static final Logger log = LoggerFactory.getLogger(HandlerProcess.class);

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Map<String, Class> map = new HashMap<>();
        ClassScaner.scan(basePackage, OrderHandlerType.class).forEach(x -> {
            String type = x.getAnnotation(OrderHandlerType.class).value();
            map.put(type, x);
        });
        beanFactory.registerSingleton(OrderHandlerType.class.getName(), map);
    }
}
